package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishMapper;
import com.sky.service.DishService;
import com.sky.vo.DishPageQueryVO;
import com.sky.vo.DishVO;
import com.sky.vo.PageResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResultVO pageList(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishPageQueryVO> page = dishMapper.pageList(dishPageQueryDTO);
        long total = page.getTotal();
        List<DishPageQueryVO> list = page.getResult();

        return new PageResultVO(total,list);
    }

    @Override
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setStatus(StatusConstant.ENABLE);
//        dish.setCreateTime(LocalDateTime.now());
//        dish.setUpdateTime(LocalDateTime.now());
//        dish.setCreateUser(BaseContext.getCurrentId());
//        dish.setUpdateUser(BaseContext.getCurrentId());
        dishMapper.save(dish);

        Long id = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor f : flavors) {
                f.setDishId(id);
            }
            dishMapper.saveFlavors(flavors);
        }

    }

    @Override
    public DishVO getById(Integer id) {
        DishVO dishVO = dishMapper.getById(id);
        List<DishFlavor> flavors = dishMapper.getFlavorsById(id);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    @Override
    public List<Dish> getByCategoryId(String categoryId) {
        return dishMapper.getByCategoryId(categoryId);
    }

    @Transactional
    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
//        dish.setUpdateTime(LocalDateTime.now());
//        dish.setUpdateUser(BaseContext.getCurrentId());


        //删除之前的dish口味
        dishMapper.deleteFlavors(Integer.parseInt(dishDTO.getId().toString()));

        dishMapper.update(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors.size()>0) {
            for (DishFlavor df : flavors) {
                df.setDishId(dishDTO.getId());
            }

            dishMapper.saveFlavors(flavors);
        }



    }

    @Override
    public void delete(String ids) {
        String[] split = ids.split(",");
        Integer[] id = new Integer[split.length];
        for (int i = 0; i < split.length; i++) {
            id[i] = Integer.parseInt(split[i]);
            dishMapper.deleteFlavors(id[i]);
        }
        dishMapper.delete(id);
    }

    @Override
    public void updateStatus(Integer status, Integer id) {
        dishMapper.updateStatus(status,id);
    }
}
